//
//  NavigationController.m
//  微博2期
//
//  Created by J on 15/11/18.
//
//

#import "NavigationController.h"

@implementation NavigationController

/**
 *  重写这个方法的目的: 能够拿到所有push进来的控制器
 *
 *  @param viewController 即将push进来的控制器
 *  %90 的拦截 都是通过自定义类 重写自定义的方法实现的
 */
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
    [super pushViewController:viewController animated:animated];
    
//    NSLog(@"%s",__func__);
//先push 然后才会加载视图  走viewController控制器的生命周期 viewController init 然后调用这个方法push 然后 loadView
    if (self.viewControllers.count>1) {//这是push进来的控制器viewController, 不是第一个子控制器(不是根控制器)
        
        viewController.hidesBottomBarWhenPushed = YES;//自动显示和隐藏tabbar push谁 谁就调用 第二个控制器的时候才隐藏
        
        //设置导航栏内容
        UIButton * backButton =[UIButton buttonWithType:UIButtonTypeCustom] ;
        [backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
        [backButton setBackgroundImage:[UIImage imageNamed:@"navigationbar_back"] forState:UIControlStateNormal];
        [backButton setBackgroundImage:[UIImage imageNamed:@"navigationbar_back_highlighted"] forState:UIControlStateHighlighted];
        backButton.size = backButton.currentBackgroundImage.size;//普通状态下图片 x,y设置多少都无用
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backButton];
    
        
        UIButton * moreButton =[UIButton buttonWithType:UIButtonTypeCustom] ;
        [moreButton addTarget:self action:@selector(more) forControlEvents:UIControlEventTouchUpInside];
        [moreButton setBackgroundImage:[UIImage imageNamed:@"navigationbar_more"] forState:UIControlStateNormal];
        [moreButton setBackgroundImage:[UIImage imageNamed:@"navigationbar_more_highlighted"] forState:UIControlStateHighlighted];
        moreButton.size = moreButton.currentBackgroundImage.size;//普通状态下图片 x,y设置多少都无用
        viewController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:moreButton];
        
    }
    
    
}


- (void)back{
    //这里sel本身就是一个导航控制器 self.navigationController ==null
    [self popViewControllerAnimated:YES];
}

- (void)more{
    [self popToRootViewControllerAnimated:YES];//返回根控制器
    
}

@end
